package com.loyal.helper;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import java.util.HashMap;
import java.util.Map;

public class PermissionHelper {
    //日历
    public static final String READ_CALENDAR = Manifest.permission.READ_CALENDAR;
    public static final String WRITE_CALENDAR = Manifest.permission.WRITE_CALENDAR;
    //相机
    public static final String CAMERA = Manifest.permission.CAMERA;
    //联系人
    public static final String READ_CONTACTS = Manifest.permission.READ_CONTACTS;
    public static final String WRITE_CONTACTS = Manifest.permission.WRITE_CONTACTS;
    public static final String GET_ACCOUNTS = Manifest.permission.GET_ACCOUNTS;
    //定位
    public static final String ACCESS_FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    public static final String ACCESS_COARSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    //录音
    public static final String RECORD_AUDIO = Manifest.permission.RECORD_AUDIO;
    //读取手机状态
    public static final String READ_PHONE_STATE = Manifest.permission.READ_PHONE_STATE;
    //存储
    public static final String READ_EXTERNAL_STORAGE = Manifest.permission.READ_EXTERNAL_STORAGE;
    public static final String WRITE_EXTERNAL_STORAGE = Manifest.permission.WRITE_EXTERNAL_STORAGE;

    /**
     * @param permission 权限名称
     * @return true:已授权/false:未授权
     */
    public static boolean checkPermission(@NonNull Context context, String permission) {
        return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(context, permission);
    }

    //--activity--
    public static void single(@NonNull AppCompatActivity activity, SingleCallback callback, String permission) {
        single(activity, callback).launch(permission);
    }

    public static ActivityResultLauncher<String> single(@NonNull AppCompatActivity activity, SingleCallback callback) {
        return activity.registerForActivityResult(new ActivityResultContracts.RequestPermission(),
                result -> {
                    if (null != callback)
                        callback.singlePermissionCallback(result);
                });
    }

    //--activity--
    public static void multiple(@NonNull AppCompatActivity activity, MultipleCallback callback, String[] permission) {
        multiple(activity, callback).launch(permission);
    }

    public static ActivityResultLauncher<String[]> multiple(@NonNull AppCompatActivity activity, MultipleCallback callback) {
        return activity.registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(),
                result -> {
                    if (null != callback)
                        callback.multiplePermissionsCallback(refuseMap(result));
                });
    }

    //--fragment--
    public static void single(@NonNull Fragment fragment, SingleCallback callback, String permission) {
        single(fragment, callback).launch(permission);
    }

    public static ActivityResultLauncher<String> single(@NonNull Fragment fragment, SingleCallback callback) {
        return fragment.registerForActivityResult(new ActivityResultContracts.RequestPermission(),
                result -> {
                    if (null != callback)
                        callback.singlePermissionCallback(result);
                });
    }

    //--fragment--
    public static void multiple(@NonNull Fragment fragment, MultipleCallback callback, String[] permission) {
        multiple(fragment, callback).launch(permission);
    }

    public static ActivityResultLauncher<String[]> multiple(@NonNull Fragment fragment, MultipleCallback callback) {
        return fragment.registerForActivityResult(new ActivityResultContracts.RequestMultiplePermissions(),
                result -> {
                    if (null != callback)
                        callback.multiplePermissionsCallback(refuseMap(result));
                });
    }

    public static @NonNull
    Map<String, Boolean> refuseMap(Map<String, Boolean> resultMap) {
        Map<String, Boolean> refuseMap = new HashMap<>();
        for (Map.Entry<String, Boolean> entry : resultMap.entrySet()) {
            if (!entry.getValue()) {
                refuseMap.put(entry.getKey(), entry.getValue());
            }
        }
        return refuseMap;
    }

    public interface MultipleCallback {
        /**
         * if(successful){
         * //成功获取权限 do something
         * }else{
         * //权限申请被拒
         * }
         *
         * @param refuseMap 拒绝权限列表
         */
        void multiplePermissionsCallback(@NonNull Map<String, Boolean> refuseMap);
    }

    public interface SingleCallback {
        /**
         * if(successful){
         * // `singlePermission.name` is granted !
         * //成功获取权限 do something
         * }else{
         * //权限申请被拒
         * }
         *
         * @param successful 权限申请是否成功
         */
        void singlePermissionCallback(boolean successful);
    }
}
